package com.yanqu.road.server.timer.union;

import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class CrossUnionSyncTask extends GeneralTask {

    public final static Queue<Long> serverList = new LinkedList<>();

    public static int patchNum = 100;

    @Override
    public void doRun() {

        List<Long> list = new ArrayList<>();
        synchronized (serverList){
            while (serverList.size() > 0){
                Long serverId = serverList.poll();
                list.add(serverId);
                if(list.size() >= patchNum){
                    break;
                }
            }
        }
        for (long serverId : list) {
            MessageHelper.sendPacket(serverId, 0, YanQuMessageUtils.buildMessage(Protocol.S_CROSS_UNION_SYNC_FROM_CROSS, CrossUnionMgr.sendAllUnionGroupData(serverId)));
        }
    }


    public static void addServerList(long serverId){
        synchronized (serverList){
            if(!serverList.contains(serverId)) {
                serverList.add(serverId);
            }
        }
    }
}
